import { Review } from 'review-model.js';

var review = new Review();
Page({
	data: {
    data:[],
    pics:[],
    spics:[],
    paths:[],
    orderid:'',
    flagFlower:[],
    flag1:0,
    flag2: 0,
    flag3: 0,
    flag4: false,
	},
  onLoad: function (options) {
    var orderid = options.id;
    this._loadData(orderid);
    console.log(this.data.data)
  },
  _loadData: function (orderid, callback) {
    var that = this;
    review.getOrder(orderid, (data) => {
 
            for(var i=0;i<data['getData'].length;i++)
            {
              that.data.paths[i]=[];
              that.data.flagFlower[i]='';
            }
            for (var i = 0; i < data.length; i++) {
              that.data.flagFlower[i]="";
            }
            
            that.setData({
              isDel: that.data.isDel,
              paths: that.data.paths,
              flagFlower: that.data.flagFlower,
              data: data['getData'],
              orderid:data['order_id'],
              paths: that.data.paths

            });
            
    });
  
  },
  changeColor6: function (e) {
    var index = e.currentTarget.dataset.index;
    this.data.flagFlower[index]=1
    this.setData({
      flagFlower: this.data.flagFlower
    });
  },
  changeColor7: function (e) {
    var index = e.currentTarget.dataset.index;
    this.data.flagFlower[index] = 2
    this.setData({
      flagFlower: this.data.flagFlower
    });
  },
  changeColor8: function (e) {
    var index = e.currentTarget.dataset.index;
    this.data.flagFlower[index] = 3
    this.setData({
      flagFlower: this.data.flagFlower
    });
  },
  changeColor1: function (e) {
    var that = this;
    var id = e.target.dataset.id
    if (id == 1) {
      that.setData({
        flag1: 1
      });
    } else if (id == 2) {
      that.setData({
        flag2: 1
      });
    } else {
      that.setData({
        flag3: 1
      });
    }


  },
  changeColor2: function (e) {
    var that = this;
    var id = e.target.dataset.id
    if (id == 1) {
      that.setData({
        flag1: 2
      });
    } else if (id == 2) {
      that.setData({
        flag2: 2
      });
    } else {
      that.setData({
        flag3: 2
      });
    }

  },
  changeColor3: function (e) {
    var that = this;
    var id = e.target.dataset.id
    if (id == 1) {
      that.setData({
        flag1: 3
      });
    } else if (id == 2) {
      that.setData({
        flag2: 3
      });
    } else {
      that.setData({
        flag3: 3
      });
    }
  },
  changeColor4: function (e) {
    var that = this;
    var id = e.target.dataset.id
    if (id == 1) {
      that.setData({
        flag1: 4
      });
    } else if (id == 2) {
      that.setData({
        flag2: 4
      });
    } else {
      that.setData({
        flag3: 4
      });
    }

  },
  changeColor5: function (e) {
    var that = this;
    var id = e.target.dataset.id
    if (id == 1) {
      that.setData({
        flag1: 5
      });
    } else if (id == 2) {
      that.setData({
        flag2: 5
      });
    } else {
      that.setData({
        flag3: 5
      });
    }
  },
  chooseImage: function (e) {
    var that = this;
    var pics=[];
     var yp=e.currentTarget.dataset.qy;
    wx.chooseImage({
      count: 3,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        var imgsrc = res.tempFilePaths;
            
        　　 pics = pics.concat(imgsrc);
        console.log(pics);
        that.setData({
          pics: pics
        });
        that.uploadimg(yp,pics);
      }
    })
  },
 uploadimg: function (yp,pics){//这里触发图片上传的方法
  
    var uploadData={};
    uploadData['url'] = 'https://program.chinlingcloud.com/index.php/api/v2/uploadimg';
    uploadData['path']=pics;
    uploadData['yp']=yp;
   this._uploadimg(uploadData,1);
    
  },
 _uploadimg: function (data, is_Whether) {
    //exit;
    var that = this;
    var i = data.i ? data.i : 0;//当前上传的哪张图片
     
    var    success = data.success ? data.success : 0;//上传成功的个数
    var    fail = data.fail ? data.fail : 0;//上传失败的个数
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'file',//这里根据自己的实际情况改
      formData: {
        'urlRoute': 'pjimg',
        'qy':data.yp,
        'is_whether':is_Whether
      },
      success: (resp) => {
        console.log(resp.data)
        var object = JSON.parse(resp.data);
        if (object.state == 1) {
             success++;//图片上传成功，图片上传成功的变量+1  
          (that.data.paths)[object.qy].push(object.path);//=that.data.spics;
          that.setData({
            paths: that.data.paths
          })
          console.log(that.data.paths);
        }
       
        
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1
      },
      fail: (res) => {
        fail++;//图片上传失败，图片上传失败的变量+1
        //console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        //console.log(i);
        i++;//这个图片执行完上传后，开始上传下一张
        if (i == data.path.length) {   //当图片传完时，停止调用          
          //console.log('执行完毕');
          //console.log('成功：' + success + " 失败：" + fail);
        } else {//若图片还没有传完，则继续调用函数
          //console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that._uploadimg(data,0);
        }

      }
    });
  },
 delImg: function (e) {
   var indexs = e.currentTarget.dataset.indexs;
   var index = e.currentTarget.dataset.index;
   this.data.paths[index].splice(indexs,1);
   this.setData({
     paths:this.data.paths

   })
   console.log(this.data.paths)
 }, 
formSubmit: function (e) {
  console.log(e);
   var postData={};
   for(var i=0;i<this.data.paths.length;i++)
   {
     postData['img'] = this.data.paths;
     postData['flagFlower'] = this.data.flagFlower;
   }
   postData['flag1']=this.data.flag1;
   postData['flag2'] = this.data.flag2;
   postData['flag3'] = this.data.flag3;
   postData['orderid']=this.data.orderid;
   postData['evaluate']=e.detail.value;
   var  postInfo=this.data.data;
   var Info={};
   Info['postData']=postData;
   Info['postInfo']=postInfo;
   console.log(Info['postData']);
   review.postEvaluate(Info, (statusCode) => {
     if (statusCode.state)
     {
       var flag=true;
       wx.navigateTo({
         url: '../pay-result/pay-result?id=' + statusCode.order_id + 'flag='+flag+'&from=pj'
       });
     }else
     {
       var flag = false;
       wx.navigateTo({
         url: '../pay-result/pay-result?id=' + statusCode.order_id + 'flag=' + flag + '&from=pj'
       });
     }
     
   });
  

 }, 
  	/*onLoad: function (options) {
      		var orderId = options.orderid;
          		var totalFee = options.totalFee;
    }
            })
      that.setData({
        data: data,
      });
    });
  },
	/*onLoad: function (options) {
		var orderId = options.orderid;
		var totalFee = options.totalFee;
		this.setData({
			orderId: orderId,
			totalFee: totalFee
		})
	},
  changeColor1: function (e) {
    var that = this;
    var id = e.target.dataset.id
    if(id==1){
      that.setData({
        flag1: 1
      });
    }else if(id==2){
      that.setData({
        flag2: 1
      });
    }else{
      that.setData({
        flag3: 1
      });
    }
    
   
  },
  changeColor2: function (e) {
    var that = this;
    var id = e.target.dataset.id
    if (id == 1) {
      that.setData({
        flag1: 2
      });
    } else if (id == 2) {
      that.setData({
        flag2: 2
      });
    } else {
      that.setData({
        flag3: 2
      });
    }

  },
  changeColor3: function (e) {
    var that = this;
    var id = e.target.dataset.id
    if (id == 1) {
      that.setData({
        flag1: 3
      });
    } else if (id == 2) {
      that.setData({
        flag2: 3
      });
    } else {
      that.setData({
        flag3: 3
      });
    }
  },
  changeColor4: function (e) {
    var that = this;
    var id = e.target.dataset.id
    if (id == 1) {
      that.setData({
        flag1: 4
      });
    } else if (id == 2) {
      that.setData({
        flag2: 4
      });
    } else {
      that.setData({
        flag3: 4
      });
    }

  },
  changeColor5: function (e) {
    var that = this;
    var id = e.target.dataset.id
    if (id == 1) {
      that.setData({
        flag1: 5
      });
    } else if (id == 2) {
      that.setData({
        flag2: 5
      });
    } else {
      that.setData({
        flag3: 5
      });
    }
  },
  changeColor6: function (e) {
    this.setData({
      flag: 1
    });
  },
  changeColor7: function (e) {
    this.setData({
      flag: 2
    });
  },
  changeColor8: function (e) {
    this.setData({
      flag: 3
    });
  },
  changeColor9: function (e) {
    var flag4_id = e.target.dataset.id
    if (flag4_id){
      this.setData({
        flag4: false
      });
    }else{
      this.setData({
        flag4: true
      });
    }
  },
  chooseImage: function (e) {
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        const uploadTask =wx.uploadFile({
          url: 'https://example.weixin.qq.com/upload', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: function (res) {
            var data = res.data
            //do something
            console.log("上传成功")
          }
        })
        uploadTask.onProgressUpdate((res) => {
          console.log('上传进度', res.progress)
          console.log('已经上传的数据长度', res.totalBytesSent)
          console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
        })
      }
    })
  }*/
})